//
//  Currency.m
//  Whopaidforit
//
//  Created by Hoang Vu on 2/17/13.
//  Copyright (c) 2013 Hoang Vu. All rights reserved.
//

#import "Currency.h"
#import "SBJsonParser.h"

@implementation Currency
static Currency *_instance = nil;  // <-- important
#define SERVER_URL @"http://nghexaydung.com/whopaid/public/"
+(Currency *)instance
{
	// skip everything
	if(_instance) return _instance;
    
	// Singleton
	@synchronized([Currency class])
	{
		if(!_instance)
		{
			_instance = [[self alloc] init];
            
            //	NSLog(@"Creating global instance!"); <-- You should see this once only in your program
		}
        
		return _instance;
	}
    
	return nil;
}
-(NSMutableArray *) getList
{
    NSMutableArray *listData = [[NSMutableArray alloc] init ];
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@/currency/getcurrency",SERVER_URL];
    NSLog(@"get currency list: %@",url);
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    NSArray *results = [parser objectWithString:json_string error:nil];
    for (NSDictionary *obj in results)
    {
        CurrencyClass *event = [[CurrencyClass alloc] init];
        event.currencyId = [[obj objectForKey:@"id"] intValue];
        event.name = [obj objectForKey:@"name"];
        event.symbol = [obj objectForKey:@"symbol"];
        [listData addObject:event];
        
    }
    return listData;
}

@end
